package com.aplikata.evok.ui;

import java.math.BigInteger;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;

import org.primefaces.event.TabChangeEvent;
import org.primefaces.model.TreeNode;

import com.aplikata.evok.bo.DoctorSchedule;
import com.aplikata.evok.bo.PatientTherapy;
import com.aplikata.evok.bo.PatientTherapyComment;
import com.aplikata.evok.constants.EvokConstants;
import com.aplikata.evok.email.EvokEmailUtil;
import com.aplikata.evok.service.EvokTherapyService;
import com.aplikata.ui.AplikataReportUIBean;
import com.aplikata.util.FacesUtil;
import com.aplikata.util.YunDateUtil;

/** 
* @ClassName: EvokPatientTherapyAbstractBean 
* @Description: TODO
* @author Huyun
* @date Oct 30, 2012 8:25:09 PM 
*  
*/
public class EvokPatientTherapyAbstractBean extends AplikataReportUIBean {
	private static final long serialVersionUID = 1L;

	@ManagedProperty(value = "#{evokTherapyService}")
	protected EvokTherapyService evokTherapyService;
	protected PatientTherapy patientTherapy;
	protected TreeNode therapyTree;
	protected int tabIndex;

	// for comment tab
	protected List<PatientTherapyComment> commentList;
	protected PatientTherapyComment comment;
	protected Integer unreadComments;
	
	//for reservation tab
	protected List<DoctorSchedule> therapyReservationList;

	@PostConstruct
	public void init() {
		patientTherapy = new PatientTherapy(getSessionUser());
		comment = new PatientTherapyComment();
	}

	public void submitComment() {
		try {
			comment.setPatientTherapy(patientTherapy);
			comment = evokTherapyService.updateComment(comment);
			//send email
			EvokEmailUtil.sendCommentEmail(comment);
			FacesUtil.addInfoMessage("comment_add_success");
			comment = new PatientTherapyComment(patientTherapy);
			commentList = null;
			commentList = this.getCommentList();
		} catch (Exception e) {
			FacesUtil.addActionException(e, log);
		}
	}

	public void onTabChange(TabChangeEvent event) {
		String title = event.getTab().getTitle().trim();
		if (title.startsWith(FacesUtil.getMessage("comment"))) {
			try {
				evokTherapyService.submitCommentReadStatus(patientTherapy);
				unreadComments = 0;
			} catch (Exception e) {
				FacesUtil.addActionListenerException(e, log);
			}
		}
	}
	
	public void addTherapyToFlash() {
		FacesContext.getCurrentInstance().getExternalContext().getFlash()
				.put(EvokConstants.FLASH_PATIENT_THERAPY, patientTherapy);
	}

	public PatientTherapy getPatientTherapy() {
		if (patientTherapy != null && patientTherapy.getStartDate() == null) {
			patientTherapy.setStartDate(YunDateUtil.getDate());
		}
		return patientTherapy;
	}

	public void setPatientTherapy(PatientTherapy patientTherapy) {
		this.patientTherapy = patientTherapy;
	}

	public EvokTherapyService getEvokTherapyService() {
		return evokTherapyService;
	}

	public void setEvokTherapyService(EvokTherapyService evokTherapyService) {
		this.evokTherapyService = evokTherapyService;
	}

	public TreeNode getTherapyTree() {
		return therapyTree;
	}

	public void setTherapyTree(TreeNode therapyTree) {
		this.therapyTree = therapyTree;
	}

	public int getTabIndex() {
		return tabIndex;
	}

	public void setTabIndex(int tabIndex) {
		this.tabIndex = tabIndex;
	}

	@SuppressWarnings("unchecked")
	public List<PatientTherapyComment> getCommentList() {
		if (commentList == null)
			commentList = (List<PatientTherapyComment>) evokTherapyService.findByNamedQuery(
					PatientTherapyComment.QY_FIND_BY_THERAPY, new Object[] { patientTherapy.getId() });
		return commentList;
	}

	public void setCommentList(List<PatientTherapyComment> commentList) {
		this.commentList = commentList;
	}

	public PatientTherapyComment getComment() {
		return comment;
	}

	public void setComment(PatientTherapyComment comment) {
		this.comment = comment;
	}

	public List<DoctorSchedule> getTherapyReservationList() {
		return therapyReservationList;
	}

	public void setTherapyReservationList(List<DoctorSchedule> therapyReservationList) {
		this.therapyReservationList = therapyReservationList;
	}

	public Integer getUnreadComments() {
		if (FacesUtil.isKeyNull(getPatientTherapy().getId())) {
			unreadComments = 0;
		} else {
			unreadComments = ((BigInteger) evokTherapyService.findSingleByNamedQuery(
					PatientTherapyComment.QY_FIND_UNREAD_COUNT, new Object[] { patientTherapy.getId(),
							this.getSessionUser().getId() })).intValue();
		}
		return unreadComments;
	}

	public void setUnreadComments(Integer unreadComments) {
		this.unreadComments = unreadComments;
	}
}
